package com.aimosheng.main.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;

import com.aimosheng.common.Constants;
import com.aimosheng.common.dialog.AbsDialogFragment;
import com.aimosheng.common.utils.DpUtil;
import com.aimosheng.main.R;

/**
 * Created by wx on 2021/03/03.
 */

public class MainUserFilterDialogFragment extends AbsDialogFragment implements View.OnClickListener {

    private ActionListener mActionListener;

    private RadioButton mBtnTypeAll;
    private RadioButton mBtnTypeAuth;
    private RadioButton mBtnTypeNotAuth;
    private int mAuth;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_main_filter_user;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        window.setWindowAnimations(R.style.bottomToTopAnim);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = DpUtil.dp2px(180);
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBtnTypeAll = (RadioButton) findViewById(R.id.btn_type_all);
        mBtnTypeAuth = (RadioButton) findViewById(R.id.btn_type_auth);
        mBtnTypeNotAuth = (RadioButton) findViewById(R.id.btn_type_not_auth);

        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        mAuth = bundle.getInt(Constants.MAIN_USER_AUTH, 0);

        if (mAuth == Constants.TYPE_ALL) {
            mBtnTypeAll.setChecked(true);
        } else if (mAuth == Constants.TYPE_AUTH) {
            mBtnTypeAuth.setChecked(true);
        } else if (mAuth == Constants.TYPE_NOT_AUTH) {
            mBtnTypeNotAuth.setChecked(true);
        }
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_close) {
            dismiss();
        } else if (i == R.id.btn_confirm) {
            confirmClick();
        }
    }

    private void confirmClick() {
        if (mBtnTypeAll.isChecked()) {
            mAuth = Constants.TYPE_ALL;
        } else if (mBtnTypeAuth.isChecked()) {
            mAuth = Constants.TYPE_AUTH;
        } else if (mBtnTypeNotAuth.isChecked()) {
            mAuth = Constants.TYPE_NOT_AUTH;
        }
        if (mActionListener != null) {
            mActionListener.onFilter(mAuth);
        }
        dismiss();
    }


    @Override
    public void onDestroy() {
        mActionListener = null;
        super.onDestroy();
    }

    public interface ActionListener {
        void onFilter(int auth);
    }

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }
}
